<?php
function genPicturesList($data){
    $ret = '<ul class="thumb-list" id="thumbList'.'">';
    foreach ($data->pictures as $picture){
        //var_dump($cover);
        //var_dump($picture->id);
        if(isset($data->cover) && $picture->id == $data->cover)
            $ret .= '<li class="cover">';
        else
            $ret .= '<li>';
        $ret .='<span>Cover</span><div class="pic">';
        $ret .= $data->getPicture($picture->id, 'small');
        $ret .= '</div>';
        $ret .='</li>';
    }
    $ret .='</ul>';

    return $ret;
}
?>

<div class="form form-horizontal">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'product-form',
	'enableAjaxValidation'=>false,
    'htmlOptions'=>array('enctype'=>'multipart/form-data'), 
)); ?>

	<div class="alert alert-info">
        <!-- <a class="close" data-dismiss="alert">×</a> -->
        Fields with <span class="required">*</span> are required.
    </div><!-- /Form filling tips -->

	<?php echo CHtml::errorSummary(
        $model,
        '<a class="close" data-dismiss="alert">×</a><h4 class="alert-heading">Please fix the errors below:</h4>',
        '',
        array('class'=>'alert alert-error',)
    ); ?><!-- /Form error summary -->

	<div class="control-group<?php echo ($form->error($model, 'category_id')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'category_id', array('class'=>'control-label')); ?>
        <div class="controls">
            <?php echo $form->dropDownList($model,'category_id',CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addCategory(); $('#modalCategory').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
            <?php echo $form->error($model,'category_id', array('class'=>'help-inline', array('class'=>'help-inline'))); ?>
        </div>
	</div>

	<div class="control-group<?php echo ($form->error($model, 'os_id')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'os_id', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->dropDownList($model,'os_id',CHtml::listData(Os::model()->findAll(), 'id', 'name')); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addOs(); $('#modalOs').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		<?php echo $form->error($model,'os_id', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'name')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'name', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>128,'class'=>'input-xlarge')); ?>
    		<?php echo $form->error($model,'name', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'model')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'model', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'model',array('size'=>60,'maxlength'=>64,'class'=>'input-xlarge')); ?>
    		<?php echo $form->error($model,'model', array('class'=>'help-inline')); ?>
    	</div>
    </div>
    
    <?php 
        // Fetch the selected colors
        $selectedColors = array();
        foreach ($model->colors as $color)
            array_push($selectedColors, $color->id);
    ?>
    <div class="control-group<?php echo ($form->error($model, 'colors')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'colors', array('class'=>'control-label')); ?>
        <div class="controls">
            <div class="portlet-content">
    		<?php echo CHtml::checkBoxList(
                'Color', 
                $selectedColors, 
                CHtml::listData(Color::model()->findAll(),'id','name'),
                array(
                    'labelWrapped'=>true,
                    'template'=>'<label class="checkbox inline">{input}{label}</label>',
                    'separator'=>'',
                    'checkAll'=>'All',
                )
            ); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addColor(); $('#modalColor').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		</div>
            <?php echo $form->error($model,'colors', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'chip')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'chip', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'chip',array('size'=>60,'maxlength'=>64,'class'=>'input-xlarge')); ?>
    		<?php echo $form->error($model,'chip', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'size')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'size', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'size',array('class'=>'input-xlarge')); ?> <code>inches</code>
    		<?php echo $form->error($model,'size', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'weight')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'weight', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'weight',array('class'=>'input-xlarge')); ?> <code>KG</code>
    		<?php echo $form->error($model,'weight', array('class'=>'help-inline')); ?>
    	</div>
    </div>

    <div class="control-group<?php echo ($form->error($model, 'display')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'display', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->dropDownList($model,'display',CHtml::listData(Display::model()->findAll(), 'id', 'name')); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addDisplay(); $('#modalDisplay').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		<?php echo $form->error($model,'display', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'storage')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'storage', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'storage',array('class'=>'input-xlarge')); ?> <code>GB</code>
    		<?php echo $form->error($model,'storage', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'memory')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'memory', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'memory',array('class'=>'input-xlarge')); ?> <code>MB</code>
    		<?php echo $form->error($model,'memory', array('class'=>'help-inline')); ?>
    	</div>
    </div>

    <div class="control-group<?php echo ($form->error($model, 'sensors')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'sensors', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->dropDownList($model,'sensors',CHtml::listData(Sensor::model()->findAll(), 'id', 'name')); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addSensors(); $('#modalSensors').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		<?php echo $form->error($model,'sensors', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<?php 
        $selectedNetworks = array();
        foreach ($model->networks as $network)
            array_push($selectedNetworks, $network->id);
    ?>
    <div class="control-group<?php echo ($form->error($model, 'networks')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'networks', array('class'=>'control-label')); ?>
        <div class="controls">
            <div class="portlet-content">
    		<?php echo CHtml::checkBoxList(
                'Network', 
                $selectedNetworks, 
                CHtml::listData(Network::model()->findAll(),'id','name'),
                array(
                    'labelWrapped'=>true,
                    'template'=>'<label class="checkbox inline">{input}{label}</label>',
                    'separator'=>'',
                    'checkAll'=>'All',
                )
            ); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addNetwork(); $('#modalNetwork').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		</div>
            <?php echo $form->error($model,'networks', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'camera')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'camera', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'camera',array('class'=>'input-xlarge')); ?> <code>Million Pixels</code>
    		<?php echo $form->error($model,'camera', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'battery')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'battery', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'battery',array('size'=>60,'maxlength'=>64,'class'=>'input-xlarge')); ?>
    		<?php echo $form->error($model,'battery', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'power')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'power', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textField($model,'power',array('size'=>60,'maxlength'=>64,'class'=>'input-xlarge')); ?>
    		<?php echo $form->error($model,'power', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'audio')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'audio', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textArea($model,'audio',array('rows'=>6, 'cols'=>50,'class'=>'input-xxlarge')); ?>
    		<?php echo $form->error($model,'audio', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'video')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'video', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textArea($model,'video',array('rows'=>6, 'cols'=>50,'class'=>'input-xxlarge')); ?>
    		<?php echo $form->error($model,'video', array('class'=>'help-inline')); ?>
    	</div>
    </div>
    
    <?php 
        $selectedIos = array();
        foreach ($model->ios as $io)
            array_push($selectedIos, $io->id);
    ?>
    <div class="control-group<?php echo ($form->error($model, 'ios')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'ios', array('class'=>'control-label')); ?>
        <div class="controls">
            <div class="portlet-content">
    		<?php echo CHtml::checkBoxList(
                'Io', 
                $selectedIos, 
                CHtml::listData(Io::model()->findAll(),'id','name'),
                array(
                    'labelWrapped'=>true,
                    'template'=>'<label class="checkbox inline">{input}{label}</label>',
                    'separator'=>'',
                    'checkAll'=>'All',
                )
            ); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addIo(); $('#modalIo').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		</div>
            <?php echo $form->error($model,'ios', array('class'=>'help-inline')); ?>
    	</div>
    </div>
    
    <?php 
        $selectedAccessories = array();
        foreach ($model->accessories as $accessory)
            array_push($selectedAccessories, $accessory->id);
    ?>
    <div class="control-group<?php echo ($form->error($model, 'accessories')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'accessories', array('class'=>'control-label')); ?>
        <div class="controls">
            <div class="portlet-content">
    		<?php echo CHtml::checkBoxList(
                'Accessory', 
                $selectedAccessories, 
                CHtml::listData(Accessory::model()->findAll(),'id','name'),
                array(
                    'labelWrapped'=>true,
                    'template'=>'<label class="checkbox inline">{input}{label}</label>',
                    'separator'=>'',
                    'checkAll'=>'All',
                )
            ); ?>
            <!--<?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addAccessory(); $('#modalAccessory').modal('show')
                    .on('hidden', function(){
                        $('#modalAccessory .modal-body').empty();
                    }); 
                }",
                'class'=>'btn btn-mini btn-primary',
            )) ?>-->
    		</div>
            <?php echo $form->error($model,'accessories', array('class'=>'help-inline')); ?>
    	</div>
    </div>
    
    <?php 
        $selectedLanguages = array();
        foreach ($model->languages as $language)
            array_push($selectedLanguages, $language->id);
    ?>
    <div class="control-group<?php echo ($form->error($model, 'languages')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'languages', array('class'=>'control-label')); ?>
        <div class="controls">
            <div class="portlet-content">
    		<?php echo CHtml::checkBoxList(
                'Language', 
                $selectedLanguages, 
                CHtml::listData(Language::model()->findAll(),'id','name'),
                array(
                    'labelWrapped'=>true,
                    'template'=>'<label class="checkbox inline">{input}{label}</label>',
                    'separator'=>'',
                    'checkAll'=>'All',
                )
            ); ?>
            <?php echo CHtml::link('<i class="icon-plus icon-white"></i>', '#', array(
                'onclick' => "{ addLanguage(); $('#modalLanguage').modal('show');}",
                'class'=>'btn btn-mini btn-primary',
            )) ?>
    		</div>
            <?php echo $form->error($model,'languages', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'extra')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'extra', array('class'=>'control-label')); ?>
        <div class="controls">
    		<?php echo $form->textArea($model,'extra',array('rows'=>6, 'cols'=>50,'class'=>'input-xxlarge')); ?>
    		<?php echo $form->error($model,'extra', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="control-group<?php echo ($form->error($model, 'thumbs')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'thumbs', array('class'=>'control-label')); ?>
        <div class="controls">
            <?php $this->widget('application.extensions.swfupload.SwfuploadWidget',
                array(
                    'debug'=>Yii::app()->params['swfuplaodDebug'], // Debug mode
                    'use_query_string'=>false,
                    'upload_url'=>$this->createUrl('productPicture/upload'),
                    'post_params'=>array(
                        'PHPSESSID'=>session_id(), 
                    ),
                    'file_size_limit'=>'2 MB',
                    'file_types'=>'*.jpg;*.png;*.gif',
                    'file_types_description'=>'Image Files',
                    'file_upload_limit'=>100,
                    'file_queue_limit'=>0,

                    // Events defined in handler.js
                    'file_queued_handler'=>'js:fileQueued',
                    'file_queue_error_handler'=>'js:fileQueueError',
                    'file_dialog_complete_handler'=>'js:fileDialogComplete',
                    'upload_start_handler'=>'js:uploadStart',
                    'upload_progress_handler'=>'js:uploadProgress',
                    'upload_error_handler'=>'js:uploadError',
                    'upload_success_handler'=>'js:uploadSuccess',
                    'upload_complete_handler'=>'js:uploadComplete',
                    //'queue_complete_handler'=>'js:queueComplete',

                    'custom_settings'=>array(
                        'progressTarget'=>'productPicUploadProgress',
                    ),
                    'button_placeholder_id'=>'productPicSwfupload',
                    'button_width'=>170,
                    'button_height'=>20,
                    'button_action'=>-110,
                )
            ); ?>
            <span class="swfupload btn"><span id="productPicSwfupload"></span></span>
            <div id="productPicUploadProgress"></div>

            <?php echo $form->error($model, 'thumbs', array('class'=>'help-inline')); ?> 
            <?php echo genPicturesList($model); ?>
        </div>
    </div>

    <div class="control-group<?php echo ($form->error($model, 'is_recommended')) ? ' error' : ''; ?>">
        <?php echo $form->labelEx($model,'is_recommended', array('class'=>'control-label')); ?>
        <div class="controls">
            <?php echo $form->textField($model,'is_recommended',array('size'=>60,'maxlength'=>128,'class'=>'input-xlarge')); ?>
            <?php echo $form->error($model,'is_recommended', array('class'=>'help-inline')); ?>
        </div>
    </div>
   
    <div class="control-group<?php echo ($form->error($model, 'details')) ? ' error' : ''; ?>">
		<?php echo $form->labelEx($model,'details', array('class'=>'control-label')); ?>
        <div class="controls">
            <?php $this->widget('application.extensions.tinymce.ETinyMce', array(
                'name'=>'details',
                'id'=>'details',
                'model'=>$model,
                'attribute'=>'details',
            )); ?>
    		<?php echo $form->error($model,'details', array('class'=>'help-inline')); ?>
    	</div>
    </div>

	<div class="form-actions">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class'=>'btn btn-large btn-primary')); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<!-- Modals -->

<!-- Modal for Creating Category -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalCategory',
    'options' => array(
        'title' => 'Create Category',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Operation System -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalOs',
    'options' => array(
        'title' => 'Create Operation System',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Display -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalDisplay',
    'options' => array(
        'title' => 'Create Display',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Sensor -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalSensors',
    'options' => array(
        'title' => 'Create Sensors',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Color -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalColor',
    'options' => array(
        'title' => 'Create Color',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Network -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalNetwork',
    'options' => array(
        'title' => 'Create Network',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Accessory -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalAccessory',
    'options' => array(
        'title' => 'Create Accessory',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Input/Output -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalIo',
    'options' => array(
        'title' => 'Create Input/Output',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Modal for Creating Language -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id' => 'modalLanguage',
    'options' => array(
        'title' => 'Create Language',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<!-- Scripts -->
<script type="text/javascript">
// Add a category by ajax
function addCategory()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('category/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalCategory div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalCategory div.modal-body form').submit(addCategory);
            }
            else
            {
                //console.log(data);
                $('#Product_category_id').append('<option value='+data.model.id+' selected=\"selected\">'+data.model.name+'</option>');
                $('#modalCategory div.modal-body').html(data.content);
                setTimeout(\"$('#modalCategory').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add a operation system by ajax
function addOs()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('os/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalOs div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalOs div.modal-body form').submit(addOs);
            }
            else
            {
                //console.log(data);
                $('#Product_os_id').append('<option value='+data.model.id+' selected=\"selected\">'+data.model.name+'</option>');
                $('#modalOs div.modal-body').html(data.content);
                setTimeout(\"$('#modalOs').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add a display by ajax
function addDisplay()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('display/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalDisplay div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalDisplay div.modal-body form').submit(addDisplay);
            }
            else
            {
                //console.log(data);
                $('#Product_display').append('<option value='+data.model.id+' selected=\"selected\">'+data.model.name+'</option>');
                $('#modalDisplay div.modal-body').html(data.content);
                setTimeout(\"$('#modalDisplay').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add sensors by ajax
function addSensors()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('sensor/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalSensors div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalSensors div.modal-body form').submit(addSensors);
            }
            else
            {
                //console.log(data);
                $('#Product_sensors').append('<option value='+data.model.id+' selected=\"selected\">'+data.model.name+'</option>');
                $('#modalSensors div.modal-body').html(data.content);
                setTimeout(\"$('#modalSensors').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add a color by ajax
function addColor()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('color/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            console.log(data);
            if (data.status == 'failure')
            {
                $('#modalColor div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalColor div.modal-body form').submit(addColor);
            }
            else
            {
                //console.log(data);
                $('#Color').append('<label class=\"checkbox inline\" for=\"Color_'+(data.model.id-1)+'\"><input value=\"'+data.model.id+'\" id=\"Color_'+(data.model.id-1)+'\" type=\"checkbox\" checked=\"checked\" name=\"Color[]\">'+data.model.name+data.model.hex+'</label>');
                $('#modalColor div.modal-body').html(data.content);
                setTimeout(\"$('#modalColor').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add a network by ajax
function addNetwork()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('network/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalNetwork div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalNetwork div.modal-body form').submit(addNetwork);
            }
            else
            {
                //console.log(data);
                $('#Network').append('<label class=\"checkbox inline\" for=\"Network_'+(data.model.id-1)+'\"><input value=\"'+data.model.id+'\" id=\"Network_'+(data.model.id-1)+'\" type=\"checkbox\" checked=\"checked\" name=\"Network[]\">'+data.model.name+'</label>');
                $('#modalNetwork div.modal-body').html(data.content);
                setTimeout(\"$('#modalNetwork').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}
 
// Add an accessory by ajax
function addAccessory()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('accessory/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalAccessory div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalAccessory div.modal-body form').submit(addAccessory);
            }
            else
            {
                //console.log(data);
                $('#Accessory').append('<label class=\"checkbox inline\" for=\"Accessory_'+(data.model.id-1)+'\"><input value=\"'+data.model.id+'\" id=\"Accessory_'+(data.model.id-1)+'\" type=\"checkbox\" checked=\"checked\" name=\"Accessory[]\">'+data.model.name+'</label>');
                $('#modalAccessory div.modal-body').html(data.content);
                setTimeout(\"$('#modalAccessory').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add an Input/Output by ajax
function addIo()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('io/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalIo div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalIo div.modal-body form').submit(addIo);
            }
            else
            {
                //console.log(data);
                $('#Io').append('<label class=\"checkbox inline\" for=\"Io_'+(data.model.id-1)+'\"><input value=\"'+data.model.id+'\" id=\"Io_'+(data.model.id-1)+'\" type=\"checkbox\" checked=\"checked\" name=\"Io[]\">'+data.model.name+'</label>');
                $('#modalIo div.modal-body').html(data.content);
                setTimeout(\"$('#modalIo').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

// Add a language by ajax
function addLanguage()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('language/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalLanguage div.modal-body').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalLanguage div.modal-body form').submit(addLanguage);
            }
            else
            {
                //console.log(data);
                $('#Language').append('<label class=\"checkbox inline\" for=\"Language_'+(data.model.id-1)+'\"><input value=\"'+data.model.id+'\" id=\"Language_'+(data.model.id-1)+'\" type=\"checkbox\" checked=\"checked\" name=\"Language[]\">'+data.model.name+'</label>');
                $('#modalLanguage div.modal-body').html(data.content);
                setTimeout(\"$('#modalLanguage').modal('hide') \",2000);
            }

        } ",
    )); ?>
    return false; 
}

</script>

<?php
// Set product cover
$setCoverUrl = $this->createUrl('product/setCover');
Yii::app()->clientScript->registerScript('setCover', "$('.thumb-list li').live('click', function(){
        var picId = $(this).find('img').attr('rel');
        var _this = $(this);
        $.ajax({
            url: '".$setCoverUrl."',
            data: {pictureId:picId},
            type: 'post',
            dataType: 'json',
            success: function(data){
                //console.log(data);
                if (data.status == 'failure')
                {

                }
                else
                {
                    _this.addClass('cover')
                        .siblings().removeClass('cover');
                }
            }
        });
    });"
);
?>
